package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.cache.db.User;
import com.zw.yzk.kabu.data.network.entity.FollowListEntity;
import com.zw.yzk.kabu.data.network.task.GetFollowTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.FollowListMapper;
import com.zw.yzk.kabu.view.view.FollowWallView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class ConcernedWallPresenter implements Presenter {

    private UseCase getFollowList;
    private AppCompatActivity activity;
    private FollowWallView myFollowView;
    private FollowListMapper mapper;

    @Inject
    public ConcernedWallPresenter(@Named("followListUseCase") UseCase getFollowList,
                                  AppCompatActivity activity, FollowListMapper mapper) {
        this.getFollowList = getFollowList;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(FollowWallView view) {
        this.myFollowView = view;
    }

    public void getFollowList(int ownerId, int start) {
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user == null) {
            return;
        }
        getFollowList.execute(new MyFollowSubscriber(activity), new GetFollowTask(ownerId, start));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getFollowList.unSubscribe();
    }

    private class MyFollowSubscriber extends BaseSubscriber<FollowListEntity> {

        public MyFollowSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(FollowListEntity entity) {
            super.onNext(entity);
            myFollowView.getFollowListSuccess(mapper.transfer(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            myFollowView.getFollowListFail();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_follow_list_failed);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }
    }
}
